const { get, post } = require('../../../utils/request.js')
const moment = require('../../../utils/moment.js')
const EventBus = require('../../../utils/eventBus.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    tipActiveAccount: false,
    showPassword: false,
    childMoney: '',
    bankAccountId: -1,
    lendMoneyAmount: '',
    startDate: '',
    lendName: '',
    borrowerIndex: -1,
    lendYearRate: 0,
    penaltyYearRate: 0,
    agreementRepayDate: '',
    operator: '',
    nowDate: '',
    remark: '',
    tags: [],
    familys: ['爸爸', '妈妈', '爷爷', '奶奶', '外公', '外婆']
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    const startDate = moment().format('YYYY-MM-DD')

    get('family/info', null, ({ family: { memberAlias }, tags, tipActiveAccount, lendYearRate, lendPenaltyYearRate }) => {
      const tagarrays = tags.filter(p => p.type == '借出')[0].tags
      this.setData({
        startDate: startDate,
        operator: memberAlias,
        lendYearRate: lendYearRate,
        penaltyYearRate: lendPenaltyYearRate,
        tipActiveAccount: tipActiveAccount,
        tags: tagarrays,
        nowDate: moment().format('YYYY年MM月DD日'),
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onClickTag({ currentTarget: { id } }) {
    const tag = this.data.tags[id];
    tag.active = !tag.active;
    this.setData({
      tags: this.data.tags
    })
  },
  onChangeHead({ detail: { activeCard:
    { bankAccount: { currentBalance, id }, memberName } } }) {

    this.data.tags.forEach(item => [
      item.active = false
    ])

    this.setData({
      lendName: memberName,
      childMoney: currentBalance,
      bankAccountId: id,
      incomeMoney: '',
      remark: '',
      tags: this.data.tags
    })
  },

  submitData() {
    if (this.data.tipActiveAccount) {
      wx.showToast({
        title: '请激活微行卡',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (this.data.bankAccountId <= 0) {
      wx.showToast({
        title: '请添加小孩信息',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (this.data.lendMoneyAmount == '' || this.data.lendMoneyAmount == 0) {
      wx.showToast({
        title: '请输入金额',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (this.data.lendMoneyAmount > this.data.childMoney) {
      wx.showToast({
        title: '填写金额过大',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (this.data.borrowerIndex == -1) {
      wx.showToast({
        title: '请填写借款人',
        icon: 'error',
        duration: 2000
      })
      return
    }

    if (this.data.agreementRepayDate == '') {
      wx.showToast({
        title: '请填写还款日',
        icon: 'error',
        duration: 2000
      })
      return
    }

    this.setData({
      showPassword: true
    })

  },
  onSubmit() {
    const tags = this.data.tags
      .filter(item => item.active)
      .map(item => item.name)
      .join(',');

    const params = {
      lendMoneyAmount: this.data.lendMoneyAmount,
      lendName: this.data.lendName,
      borrowerName: this.data.familys[this.data.borrowerIndex],
      lendYearRate: this.data.lendYearRate,
      penaltyYearRate: this.data.penaltyYearRate,
      agreementRepayDate: this.data.agreementRepayDate,
      operator: this.data.operator,
      remark: this.data.remark,
      tag: tags,
      'bankAccount.id': this.data.bankAccountId
    }
    get('lendrecord/save', params, () => {
      EventBus.emit('home.freshAllData');
      setTimeout(() => {
        wx.switchTab({
          url: '/pages/tabBar/summary/home',
        })
      }, 2000);
      wx.showToast({
        title: '成功',
        icon: 'success',
        duration: 2000
      })
    })
  }
})